package com.sgb.contract.view.ui.contract;

import android.text.TextUtils;

import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.contract.api.AbstractCallBack;
import com.sgb.contract.api.ContractNet;
import com.sgb.contract.base.viewmodel.BaseViewModel;
import com.sgb.contract.model.ContractListSearchBean;
import com.sgb.contract.utils.ArouterConfigs;
import com.swgk.core.base.model.entity.BaseEntity;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 描述：合同-
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.11.04
 */
public class ContractItemModel extends BaseViewModel {

    private int page = 1;
    public ContractItemAdapter adapter;
    /**
     * tab 选项卡选中状态
     */
    public ObservableInt selectTab = new ObservableInt(0);
    /**
     * tab 审批状态-选项卡选中状态
     */
    public ObservableBoolean selectTab5 = new ObservableBoolean(false);
    /**
     * tab 合同状态-选项卡选中状态
     */
    public ObservableBoolean selectTab6 = new ObservableBoolean(false);

    /**
     * vm中全局的查询合同的条件
     */
    private final ObservableField<Map<String, Object>> selectContractListMap = new ObservableField<>(new HashMap<>());
    /**
     * 列表数据更新
     */
    public MutableLiveData<Boolean> hasNoContractListData = new MutableLiveData<>();
    /**
     * 合同分类
     */
    public int category;

    public ContractItemModel() {
        adapter = new ContractItemAdapter(application.getApplicationContext());
    }

    /**
     * 获取合同分类列表数据
     */
    public void getContractList(boolean isLoadMore) {
        Map<String, Object> map = selectContractListMap.get();
        map.put("page", page = isLoadMore ? ++page : 1);
        map.put("limit", 20);
        map.put("backlog", 1);
        map.put("source", category);

        showLoading(true);
        ContractNet.getInstance().getContractApi().contractListSearch(map).enqueue(new AbstractCallBack<BaseEntity<ContractListSearchBean>>() {
            @Override
            public void onSuccess(BaseEntity<ContractListSearchBean> data) {
                if (1 == page && data.getData().list.size() <= 0) {
                    hasNoContractListData.postValue(true);
                }
                if (!isLoadMore) {
                    adapter.setDatas(data.getData().list);
                } else {
                    adapter.addDatas(data.getData().list);
                    adapter.setNoMore(data.getData().list.size() <= 0);
                }
            }

            @Override
            public void onFinally() {
                super.onFinally();
                adapter.refreshComplete();
                adapter.loadMoreComplete();
                adapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 根据合同审批状态搜索合同
     *
     * @param approval 审批状态
     */
    public void getContractListForApproval(String approval) {
        if ("-1".equals(approval)) {
            // 全部
            Objects.requireNonNull(selectContractListMap.get()).remove("auditStatus");
            getContractList(false);
        } else {
            Map<String, Object> map = selectContractListMap.get();
            map.put("page", 1);
            map.put("limit", 20);
            map.put("backlog", 1);
            map.put("auditStatus", approval);
            getContractListForBean(map);
        }
    }

    /**
     * 根据合同状态搜索合同
     *
     * @param status 合同状态
     */
    public void getContractListForStatus(String status) {
        if ("-1".equals(status)) {
            // 全部
            Objects.requireNonNull(selectContractListMap.get()).remove("status");
            getContractList(false);
        } else {
            Map<String, Object> map = selectContractListMap.get();
            map.put("page", 1);
            map.put("limit", 20);
            map.put("backlog", 1);
            map.put("status", status);
            getContractListForBean(map);
        }
    }

    /**
     * 根据合同名称搜索合同
     *
     * @param title 合同名称
     */
    public void getContractListForTitle(String title) {
        Map<String, Object> map = selectContractListMap.get();
        map.put("page", 1);
        map.put("limit", 20);
        map.put("backlog", 1);
        map.put("title", title);
        getContractListForBean(map);
    }

    /**
     * 查询合同列表
     *
     * @param map
     */
    public void getContractListForBean(Map<String, Object> map) {
        showLoading(true);
        ContractNet.getInstance().getContractApi().contractListSearch(map).enqueue(new AbstractCallBack<BaseEntity<ContractListSearchBean>>() {
            @Override
            public void onSuccess(BaseEntity<ContractListSearchBean> data) {
                if (null != data.getData().list && data.getData().list.size() <= 0) {
                    hasNoContractListData.postValue(true);
                }
                adapter.setDatas(data.getData().list);
                adapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 跳转到合同详情（签署合同）
     */
    public void jumpToSignContractActivity(ContractListSearchBean.ContractListInfo bean) {
        if (null == bean.localUrl) {
            showDialog("", "当前合同暂无内容\n请在电脑端编辑后再操作！");
            return;
        }
        if (1 == bean.status && bean.isTaker) {
            ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                    .withBoolean("isShowOperationLayout", false)
                    .withString("contractId", String.valueOf(bean.id))
                    .navigation();
        } else if (2 == bean.status && 6 == bean.appSignTag) {
            ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                    .withBoolean("isShowOperationLayout", false)
                    .withString("contractId", String.valueOf(bean.id))
                    .navigation();
        } else if (2 == bean.status && 7 == bean.appSignTag) {
            ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                    .withString("contractId", String.valueOf(bean.id))
                    .navigation();
        } else if (3 == bean.status || 4 == bean.status || 5 == bean.status) {
            ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                    .withBoolean("isShowOperationLayout", false)
                    .withString("contractId", String.valueOf(bean.id))
                    .navigation();
        } else if (1 == bean.status && !TextUtils.isEmpty(bean.deadline)) {
            if (bean.isTaker) {
                ARouter.getInstance().build(ArouterConfigs.AR_SIGN_CONTRACT)
                        .withBoolean("isShowOperationLayout", false)
                        .withString("contractId", String.valueOf(bean.id))
                        .navigation();
            } else {
                showDialog("takerEditContract", "当前合同发起方修改中");
            }
        }
    }

}